package net.ojbk.study.curriculumDesign.web.handlers.impl.canvasmodel;

import net.ojbk.study.curriculumDesign.beans.APIResult;
import net.ojbk.study.curriculumDesign.beans.canvasmodel.Message;
import net.ojbk.study.curriculumDesign.beans.usermanage.User;
import net.ojbk.study.curriculumDesign.common.api.APICodeConstant;
import net.ojbk.study.curriculumDesign.service.impl.canvasmodel.MessageServiceImpl;
import net.ojbk.study.curriculumDesign.web.handlers.canvasmodel.IMessageContraller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("message")
public class MessageControllerImpl implements IMessageContraller {

    @Autowired
    private MessageServiceImpl messageService;

    @Autowired
    private HttpSession session;

    @RequestMapping("/insert")
    public String PromptInsert(){
        return "pre/add_message";
    }
    /**
     * @author hongfive
     * @date 2019/1/29
      * @param cid
     * @return 这个油画里的留言
     */
    @RequestMapping("/msg")
    @ResponseBody
    @Override
    public Object getMsgByCid(Integer cid) {
        System.out.println("msg的cid为"+cid);
        List<Message> messages =messageService.getmsgBycid(cid);
        return APIResult.createSuccess(new APICodeConstant(),messages);
    }
    /**
     *功能描述
     * @author hongfive
     * @date 2019/1/30
      * @param message
     * @return
     */
    @RequestMapping("/addmsg")
    @ResponseBody
    @Override
    public Object addMsg(@RequestBody Message message){
        User user= null;
        user= (User) session.getAttribute("user");
        System.out.println("Uid"+user.getUid());
        message.setUid(user.getUid());
        System.out.println("用户名"+user.getUname());
        message.setUsername(user.getUname());
        messageService.addMessage(message);
        return APIResult.createError(new APICodeConstant(),"留言成功！");
    }

}
